package com.fingard.dsp.bank.directbank.qlb02;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;

/**
 * @author Parker on 2019-10-10.
 * @version 1.0
 */
public class QLB02Base extends DirectBase implements ITcpClient {
    private String separator = "#";

    public StringBuilder writerPayFileContext(ReqBatPayRec p_req9188) throws Exception {
        StringBuilder msgXml = new StringBuilder();
        msgXml.append(p_req9188.reqBatchNO).append(separator);
        msgXml.append(p_req9188.getTotalNum()).append(separator);
        msgXml.append(p_req9188.getTotalAmt()).append(separator);
        msgXml.append(p_req9188.ownAct).append(separator);
        msgXml.append(p_req9188.abstractATS).append(separator);
        msgXml.append(separator);
        msgXml.append("\r\n");
        while (p_req9188.readRecord()) {
            msgXml.append(p_req9188.readedDetail.payAct).append(separator);
            msgXml.append(p_req9188.readedDetail.payName).append(separator);
            msgXml.append(p_req9188.readedDetail.payAmount).append(separator);
            msgXml.append(p_req9188.readedDetail.abstractATS).append(separator);//支付对账码
            msgXml.append(p_req9188.readedDetail.askNumber).append(separator);//支付流水号
            msgXml.append(separator);//支付顺序号
            msgXml.append(p_req9188.readedDetail.memo).append(separator);//摘要
            msgXml.append(p_req9188.readedDetail.cellPhone).append(separator);//手机号
            msgXml.append(p_req9188.readedDetail.certNum).append("\r\n");//身份证号  为空
        }
        return msgXml;
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        WriteBankUrlLogLn("请求银行的地址" + bankFront.serverIP + ":" + bankFront.serverPort);
        WriteBankLogStep2(p_reqStr);
        //发送银行前置
        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
        //TcpPortClient tmpClient = new TcpPortClient("127.0.0.1", Integer.parseInt("7002"), this);
        tmpClient.timeoutMil = 300000;
        TcpReceiver tmpReceiver = tmpClient.sendMessage(p_reqStr, getCharset());
        retStr[0] = tmpReceiver.connState;
        retStr[1] = tmpReceiver.getFullString(getCharset());
        if(StringHelper.isNullOrEmpty(retStr[1])){
        	retStr[1] = tmpReceiver.getConnMsg();
        }
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    protected String getCharset() {
        return getBankConfig().getCharset("gbk");
    }

    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            String tmpCharset = getCharset();
            String tmpRecNow = new String(p_receiver.recBytes, 0, p_receiver.readedLength, tmpCharset);
            if (tmpRecNow.contains("</stream>")) {
                p_receiver.contentLength = p_receiver.readedLength;
                p_receiver.contentStartIndex = 0;
            }
        }
    }
}
